<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        .box {
            width: 200px;
            height: 300px;
            border: 1px solid black;
            position: relative;
            display: none;
        }
        
        body {
            height: 4000px;
        }
        
        #close {
            position: absolute;
            right: 0;
            top: 0;
        }
    </style>

</head>

<body>
    <button id="but">点击</button>
    <div class="box">
        <div id="close">×</div>
    </div>
</body>
<script src="../js/jquery.min.js"></script>
<script>
    $(() => {
        var box = $('.box');
        var close = $('#close');
        var but = $('#but');
        var top = (($(window).height() - $('.box').outerHeight()) / 2) + $(window).scrollTop();
        var left = $(window).width() / 2 - $('.box').width() / 2;
        box.css('left', left);
        box.css('top', top);
        but.click(() => {
            box.css('display', 'block');

        });

        close.click(() => {
            box.css('display', 'none');

        });
        $(window).on('scroll', function() {
            var top = (($(window).height() - $('.box').outerHeight()) / 2) + $(window).scrollTop();
            var left = $(window).width() / 2 - $('.box').width() / 2;
            box.css('left', left);
            box.css('top', top);
        });


        $(window).on('resize', function() {
            var top = (($(window).height() - $('.box').outerHeight()) / 2) + $(window).scrollTop();
            var left = $(window).width() / 2 - $('.box').width() / 2;
            box.css('left', left);
            box.css('top', top);
        });


    })
</script>

</html>